package com.dzk.cp.c11x5.action;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.dzk.cp.c11x5.index.cs.by.CsByxIndex;
import com.dzk.cp.c11x5.index.q2.hz.Q2HZIndex;
import com.dzk.cp.c11x5.index.q2.hz.yl.Q2HZnYLIndex;
import com.dzk.cp.c11x5.index.q2.kd.Q2KDIndex;
import com.dzk.cp.c11x5.index.q2.kd.yl.Q2KDnYLIndex;
import com.dzk.cp.c11x5.index.q2.l012.Q2LxxIndex;
import com.dzk.cp.c11x5.index.q2.l012.yl.Q2LxxYLIndex;
import com.dzk.cp.c11x5.index.q3.l012.Q3LxxxIndex;
import com.dzk.cp.c11x5.index.q3.l012.yl.Q3LxxxYLIndex;
import com.dzk.cp.c11x5.index.rx.cs.RXnCSIndex;
import com.dzk.cp.c11x5.index.rx.yl.RXnYLIndex;
import com.dzk.cp.c11x5.index.zhx.cs.ZHXnCSIndex;
import com.dzk.cp.c11x5.index.zhx.yl.ZHXnYLIndex;
import com.dzk.cp.c11x5.index.zx.cs.ZXnCSIndex;
import com.dzk.cp.c11x5.index.zx.yl.ZXnYLIndex;

@Controller("indexAction")
@Scope("prototype")
public class IndexAction extends BaseAction {

	private static final long serialVersionUID = -3073616633059300983L;
	
	private static final Logger log = Logger.getLogger(IndexAction.class);
	
	private int num;
	
	private int unit;
	
	private int xn;
	
	private String xc;
	
	private Map<?, Integer> data;
	
	public String rx_cs() {
		log.info("任选出现次数统计: code=" + code + ", num=" + num + ", unit=" + unit + ", xn=" + xn);
		if (xn < 1 || xn > 9) {
			throw new RuntimeException("Unsupported index!");
		}
		data = RXnCSIndex.getInstance(code, num, unit, xn).index();
		return resultString();
	}
	
	public String rx_yl() {
		log.info("任选遗漏统计: code=" + code + ", num=" + num + ", unit=" + unit + ", xc=" + xc);
		if (xc == null || xc.trim() == "") {
			throw new RuntimeException("The parameter 'xc' is needed!");
		}
		data = RXnYLIndex.getInstance(code, num, unit, convert(xc)).index();
		return resultString();
	}
	
	public String zhx_cs() {
		log.info("直选出现次数统计: code=" + code + ", num=" + num + ", unit=" + unit + ", xn=" + xn);
		if (xn < 1 || xn > 5) {
			throw new RuntimeException("Unsupported index!");
		}
		data = ZHXnCSIndex.getInstance(code, num, unit, xn).index();
		return resultString();
	}
	
	public String zhx_yl() {
		log.info("直选遗漏统计: code=" + code + ", num=" + num + ", unit=" + unit + ", xc=" + xc);
		if (xc == null || xc.trim() == "") {
			throw new RuntimeException("The parameter 'xc' is needed!");
		}
		data = ZHXnYLIndex.getInstance(code, num, unit, convert(xc)).index();
		return resultString();
	}
	
	public String zx_cs() {
		log.info("组选出现次数统计: code=" + code + ", num=" + num + ", unit=" + unit + ", xn=" + xn);
		if (xn < 1 || xn > 3) {
			throw new RuntimeException("Unsupported index!");
		}
		data = ZXnCSIndex.getInstance(code, num, unit, xn).index();
		return resultString();
	}
	
	public String zx_yl() {
		log.info("组选遗漏统计: code=" + code + ", num=" + num + ", unit=" + unit + ", xc=" + xc);
		if (xc == null || xc.trim() == "") {
			throw new RuntimeException("The parameter 'xc' is needed!");
		}
		data = ZXnYLIndex.getInstance(code, num, unit, convert(xc)).index();
		return resultString();
	}
	
	public String cs_by_rx() {
		log.info("任选每*出现次数统计: code=" + code + ", num=" + num + ", unit=" + unit + ", xc=" + xc);
		if (xc == null || xc.trim() == "") {
			throw new RuntimeException("The parameter 'xc' is needed!");
		}
		data = CsByxIndex.getInstance(code, num, unit, convert(xc), 1).index();
		return resultString();
	}
	
	public String cs_by_zhx() {
		log.info("直选每*出现次数统计: code=" + code + ", num=" + num + ", unit=" + unit + ", xc=" + xc);
		if (xc == null || xc.trim() == "") {
			throw new RuntimeException("The parameter 'xc' is needed!");
		}
		data = CsByxIndex.getInstance(code, num, unit, convert(xc), 2).index();
		return resultString();
	}
	
	public String cs_by_zx() {
		log.info("组选每*出现次数统计: code=" + code + ", num=" + num + ", unit=" + unit + ", xc=" + xc);
		if (xc == null || xc.trim() == "") {
			throw new RuntimeException("The parameter 'xc' is needed!");
		}
		data = CsByxIndex.getInstance(code, num, unit, convert(xc), 3).index();
		return resultString();
	}
	
	public String q2_kd() {
		log.info("前二跨度走势: code=" + code + ", num=" + num + ", unit=" + unit);
		data = Q2KDIndex.getInstance(code, num, unit).index();
		return resultString();
	}
	
	public String q2_kd_yl() {
		log.info("前二跨度遗漏统计: code=" + code + ", num=" + num + ", unit=" + unit + ", xn=" + xn);
		if (xn < 1 || xn > 10) {
			throw new RuntimeException("Unsupported index!");
		}
		data = Q2KDnYLIndex.getInstance(code, num, unit, xn).index();
		return resultString();
	}
	
	public String q2_hz() {
		log.info("前二和值走势: code=" + code + ", num=" + num + ", unit=" + unit);
		data = Q2HZIndex.getInstance(code, num, unit).index();
		return resultString();
	}
	
	public String q2_hz_yl() {
		log.info("前二和值遗漏统计: code=" + code + ", num=" + num + ", unit=" + unit + ", xn=" + xn);
		if (xn < 3 || xn > 21) {
			throw new RuntimeException("Unsupported index!");
		}
		data = Q2HZnYLIndex.getInstance(code, num, unit, xn).index();
		return resultString();
	}
	
	public String q2_l012() {
		log.info("前二012路走势: code=" + code + ", num=" + num + ", unit=" + unit);
		data = Q2LxxIndex.getInstance(code, num, unit).index();
		return resultString();
	}
	
	public String q2_l012_yl() {
		log.info("前二012路遗漏统计: code=" + code + ", num=" + num + ", unit=" + unit + ", xc=" + xc);
		if (xc == null || xc.trim() == "") {
			throw new RuntimeException("Unsupported index!");
		}
		data = Q2LxxYLIndex.getInstance(code, num, unit, xc).index();
		return resultString();
	}
	
	public String q3_l012() {
		log.info("前三012路走势: code=" + code + ", num=" + num + ", unit=" + unit);
		data = Q3LxxxIndex.getInstance(code, num, unit).index();
		return resultString();
	}
	
	public String q3_l012_yl() {
		log.info("前三012路遗漏统计: code=" + code + ", num=" + num + ", unit=" + unit + ", xc=" + xc);
		if (xc == null || xc.trim() == "") {
			throw new RuntimeException("Unsupported index!");
		}
		data = Q3LxxxYLIndex.getInstance(code, num, unit, xc).index();
		return resultString();
	}
	
	private String resultString() {
		return super.getRequest().getRequestURI().indexOf("-chart") == -1 ? "success" : "chart";
	}
	
	private String convert(String xc) {
		StringBuilder strBuilder = new StringBuilder();
		for (int i = 0; i < xc.length() >> 1; i++) {
			strBuilder.append(" ").append(xc.substring(i << 1, (i << 1) + 2));
		}
		return strBuilder.substring(1);
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public int getUnit() {
		return unit;
	}

	public void setUnit(int unit) {
		this.unit = unit;
	}

	public int getXn() {
		return xn;
	}

	public void setXn(int xn) {
		this.xn = xn;
	}

	public String getXc() {
		return xc;
	}

	public void setXc(String xc) {
		this.xc = xc;
	}

	public Map<?, Integer> getData() {
		return data;
	}

	public void setData(Map<?, Integer> data) {
		this.data = data;
	}

}
